// Container.cc

/*
   Copyright 2006 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <Container.h>
#include <Insets.h>
#include <LayoutManager.h>
#include <Graphics.h>

namespace gui {

Container::Container(void) : insets(0, 0, 0, 0) {
	components = new RefComponent[10];
	capacity = 0;
	ncomponents = 0;
	
	layoutManager = RefLayoutManager(0);
}

Container::Container(RefContainer c) : insets(c->insets.top, c->insets.left,
	c->insets.bottom, c->insets.right) {
	components = new RefComponent[c->capacity];
	capacity = c->capacity;
	ncomponents = c->ncomponents;
	
	for (int i=0; i < ncomponents; i++) {
		components[i] = c->components[i];
	}
	
	layoutManager = c->layoutManager;
}

Container::~Container(void) {
	delete components;
}

void Container::add(RefComponent c) {
	if (ncomponents == capacity) {
		RefComponent *ac = new RefComponent[capacity + 10];
		
		for (int i=0; i < capacity; i++) {
			ac[i] = components[i];
			components[i] = RefComponent(0);
		}
		
		delete [] components;
		components = ac;
		
		capacity += 10;
	}
	
	components[ncomponents++] = c;
	c->setParent(self);
}

int Container::getComponentCount(void) {
	return ncomponents;
}

RefComponent Container::getComponent(int index) {
	return components[index];
}

Insets Container::getInsets(void) {
	return insets;
}

void Container::setInsets(Insets &i) {
	insets.top = i.top;
	insets.left = i.left;
	insets.bottom = i.bottom;
	insets.right = i.right;
}

// from Component
void Container::validate(void) {
	if (!valid) {
		layout();
		
		for (int i=0; i < ncomponents; i++) {
			components[i]->validate();
		}
		
		valid = true;
	}
}

void Container::invalidate(void) {
	Component::invalidate();
}

void Container::layout(void) {
	//sd_printf("Container::layout\n");
	
	if (layoutManager != 0) {
		layoutManager->layoutContainer(self);
	}
}

void Container::setLayout(RefLayoutManager lm) {
	layoutManager = lm;
}

void Container::repaint(RefGraphics g) {
	//sd_printf("Container::repaint\n");
	
	Component::repaint(g);
}

void Container::paint(RefGraphics g) {
	//Component::paint(g);
	
	for (int i=0; i < ncomponents; i++) {
		Rectangle r;
		components[i]->getBounds(r);
		
		/*sd_printf("Container::paint: bounds %d, %d, %d, %d\n", r.x, r.y,
			r.width, r.height);*/
		
		RefGraphics g2 = g->create(r.x1, r.y1, r.x2 - r.x1, r.y2 - r.y1);
		components[i]->repaint(g2);
	}
}

} // namespace gui
